package com.huirui.server.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huirui.server.exception.BaseException;
import com.huirui.server.mapper.ZoneMapper;
import com.huirui.server.pojo.entity.Zone;
import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.vo.ZoneVo;
import com.huirui.server.service.ZoneService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author yujun
 * @description 针对表【t_zone】的数据库操作Service实现
 * @createDate 2025-11-13 15:21:24
 */
@Service
@Slf4j
public class ZoneServiceImpl extends ServiceImpl<ZoneMapper, Zone>
        implements ZoneService {

    @Override
    public PageResult<ZoneVo> listZoneItem(String keyword, Long page, Long pageSize) {
        IPage<ZoneVo> iPage = new Page<>(page, pageSize);
        iPage = baseMapper.listZoneItem(keyword, iPage);
        return PageResult.from(iPage);
    }

    @Override
    public void updateZoneItem(Long categoryId, Integer status) {
        lambdaUpdate()
                .eq(Zone::getCategoryId, categoryId)
                .set(Zone::getStatus, status)
                .set(Zone::getUpdateTime, LocalDateTime.now())
                .update();
    }

    @Override
    public void addNewZoneItem(Long categoryId, Integer status) {
        boolean exists = lambdaQuery()
                .eq(Zone::getCategoryId, categoryId)
                .exists();
        if (exists) {
            throw new BaseException("请勿重复添加！");
        }
        Zone zone = new Zone();
        zone.setCategoryId(categoryId);
        zone.setStatus(status);
        zone.setUpdateTime(LocalDateTime.now());
        zone.setCreateTime(LocalDateTime.now());
        baseMapper.insert(zone);
    }

    @Override
    public void deleteZoneItem(Long categoryId) {
        lambdaUpdate()
                .eq(Zone::getCategoryId, categoryId)
                .remove();
    }

    @Override
    public List<ZoneVo> listAvailableZoneItem() {
        return baseMapper.listAvailableZoneItem();
    }
}




